﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GZ.Entity;
using System.Data;
public partial class detail : System.Web.UI.Page
{
    public Int32 id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!Int32.TryParse(Request.QueryString["id"], out id))
            {
                Response.Redirect("/");
            }
            Init_ProductInfoById(id);
        }

    }
    /// <summary>
    ///初始化产品信息
    /// </summary>
    /// <param name="id"></param>
    private void Init_ProductInfoById(Int32 id)
    {
        EProductInfo productAccessor = new EProductInfo();
        EProductInfo product = productAccessor.GetProductInfoById(id);
        if (product != null)
        {
            lblProName.Text = product.ProductName.Trim();
            lblProEName.Text = product.ProductEName.Trim();
            lblProPrice.Text = product.Price.ToString();
            lblProStyle.Text = product.ProductStyle.ToString();
            lblProDesc.Text = product.ProductDescription.Trim();
            Init_ProductCate(product.CategoryId);
            Init_ProductThumbs(product);
            Init_Nav(product);
            Init_ProTaste(product);
            Init_PageSEO(product);
        }
        else
        {
            Response.Redirect("/");
        }
    }
    /// <summary>
    /// 初始化产品分类
    /// </summary>
    /// <param name="cid"></param>
    public void Init_ProductCate(Int32 cid)
    {
        System.Text.StringBuilder cates = new System.Text.StringBuilder();
        cates.Append("   <span><a onfocus=\"this.blur();\" href=\"/\" target=\"_blank\"><img border=\"0\" src=\"images/gz_sy_05.gif\" width=\"197\" height=\"66\"></a></span>");
        cates.AppendFormat("<span>{0}</span>", cid == 1 ? "<a href=\"juice.aspx\"><img border=\"0px\" width=\"146\" height=\"66\" src=\"images/gz_sy_10.gif\"></a>" : "<a onfocus=\"this.blur();\" onmouseover=\"MM_swapImage('juice','','images/gz_sy_10.gif',1)\" onmouseout=\"MM_swapImgRestore()\" href=\"juice.aspx\"><img width=\"146\" height=\"66\" border=\"0\" id=\"juice\" name=\"juice\" src=\"/images/gz_xz_10.gif\"></a>");
        cates.AppendFormat("<span>{0}</span>", cid == 2 ? "<a href=\"dessert.aspx\"><img border=\"0px\" width=\"143\" height=\"66\" src=\"images/gz_sy_11.gif\"></a>" : "<a onfocus=\"this.blur();\" onmouseover=\"MM_swapImage('dessert','','images/gz_sy_11.gif',1)\" onmouseout=\"MM_swapImgRestore()\" href=\"dessert.aspx\"><img width=\"143\" height=\"66\" border=\"0\" id=\"dessert\" name=\"dessert\" src=\"/images/gz_xz_11.gif\"></a>");
        cates.AppendFormat("<span>{0}</span>", cid == 3 ? "<a href=\"meal.aspx\"><img border=\"0px\" width=\"142\" height=\"66\" src=\"images/gz_sy_12.gif\"></a>" : "<a onfocus=\"this.blur();\" onmouseover=\"MM_swapImage('meal','','images/gz_sy_12.gif',1)\" onmouseout=\"MM_swapImgRestore()\" href=\"meal.aspx\"><img width=\"142\" height=\"66\" border=\"0\" id=\"meal\" name=\"meal\" src=\"/images/gz_xz_12.gif\"></a>");
        cates.AppendFormat("<span>{0}</span>", cid == 4 ? "<a href=\"special.aspx\"><img border=\"0px\" width=\"142\" height=\"66\" src=\"images/gz_sy_13.gif\"></a>" : "<a onfocus=\"this.blur();\" onmouseover=\"MM_swapImage('special','','images/gz_sy_13.gif',1)\" onmouseout=\"MM_swapImgRestore()\" href=\"special.aspx\"><img width=\"142\" height=\"66\" border=\"0\" id=\"special\" name=\"special\" src=\"/images/gz_xz_13.gif\"></a>");

        lblProCate.InnerHtml = cates.ToString();
    }
    /// <summary>
    /// 初始化缩略图
    /// </summary>
    /// <param name="product"></param>
    private void Init_ProductThumbs(EProductInfo product)
    {

        lblProPic.Text = String.Format("<img id=\"current_img\" class=\"jqzoom\"  height=\"289\" src=\"{0}\"  alt=\"{0}\" width=\"464\">", product.ImgUrl2);
        System.Text.StringBuilder thumbs = new System.Text.StringBuilder();
        thumbs.AppendFormat("{0}", !String.IsNullOrEmpty(product.ImgUrl2) ? "<li><img onmouseover=\"showImage(this)\" src=\"" + product.ImgUrl2 + "\" alt=\"" + product.ImgUrl2 + "\" width=\"60px\" height=\"60px\" border=\"0\"></li>" : "");
        thumbs.AppendFormat("{0}", !String.IsNullOrEmpty(product.ImgUrl3) ? "<li><img onmouseover=\"showImage(this)\" src=\"" + product.ImgUrl3 + "\" alt=\"" + product.ImgUrl3 + "\" width=\"60px\" height=\"60px\" border=\"0\"></li>" : "");
        thumbs.AppendFormat("{0}", !String.IsNullOrEmpty(product.ImgUrl4) ? "<li><img onmouseover=\"showImage(this)\" src=\"" + product.ImgUrl4 + "\" alt=\"" + product.ImgUrl4 + "\" width=\"60px\" height=\"60px\" border=\"0\"></li>" : "");
        thumbs.AppendFormat("{0}", !String.IsNullOrEmpty(product.ImgUrl5) ? "<li><img onmouseover=\"showImage(this)\" src=\"" + product.ImgUrl5 + "\" alt=\"" + product.ImgUrl5 + "\" width=\"60px\" height=\"60px\" border=\"0\"></li>" : "");
        if (thumbs.Length != 0)
        {
            this.lblProThumbs.Text = String.Concat("<ul>", thumbs.ToString(), "</ul>");
        }
        else
        {
            this.lblProThumbs.Text = thumbs.ToString();
        }
    }
    private void Init_PageSEO(EProductInfo product)
    {
        lblSEOTitle.Text = "<title>果之-" + product.ProductName + "</title>";
    }
    /// <summary>
    /// 初始化页面导航
    /// </summary>
    /// <param name="product"></param>
    private void Init_Nav(EProductInfo product)
    {
        try
        {
            ECategory ecAccessor = new ECategory();
            DataRow row = ecAccessor.GetCategory().Select("id=" + product.CategoryId)[0];
            String catePath = String.Empty;
            switch (row["id"].ToString())
            {
                case "1":
                    catePath = "<a style=\"color:Black; text-decoration:none;\" href=\"juice.aspx\" target=\"_bank\">" + row["CategoryName"].ToString() + "</a>";
                    break;
                case "2":
                    catePath = "<a style=\"color:Black; text-decoration:none;\" href=\"dessert.aspx\"  target=\"_bank\">" + row["CategoryName"].ToString() + "</a>";
                    break;
                case "3":
                    catePath = "<a style=\"color:Black; text-decoration:none;\" href=\"meal.aspx\"  target=\"_bank\">" + row["CategoryName"].ToString() + "</a>";
                    break;
                case "4":
                    catePath = "<a style=\"color:Black; text-decoration:none;\"href=\"special.aspx\"  target=\"_bank\">" + row["CategoryName"].ToString() + "</a>";
                    break;
            }
            lblPath.Text = String.Format("{0}/{1}", catePath, product.ProductName + (String.IsNullOrEmpty(product.ProductEName) ? "" : "(" + product.ProductEName + ")"));
        }
        catch
        {
            lblPath.Text = "";
        }
    }
    /// <summary>
    /// 初始化产品口味
    /// </summary>
    /// <param name="product"></param>
    private void Init_ProTaste(EProductInfo product)
    {
        System.Text.StringBuilder strTastes = new System.Text.StringBuilder();
        Etaste tasteAccessor = new Etaste();

        DataTable tastes = tasteAccessor.GetTasteByProductId(product.Id);
        if (tastes != null && tastes.Rows.Count != 0)
        {
            String tasteImgURL = String.Empty;
            int i = 0;
            foreach (DataRow t in tastes.Rows)
            {
                //初始化不同口味对应的口味渐变图URL
                //switch (t["Id"].ToString())
                //{
                //    case "1":
                //        tasteImgURL = "gz_sp_34.gif";
                //        break;
                //    case "2":
                //        tasteImgURL = "gz_sp_58.gif";
                //        break;
                //    case "3":
                //        tasteImgURL = "gz_sp_58.gif";
                //        break;
                //}
                if (i == 0)
                {
                    tasteImgURL = "gz_sp_34.gif";
                }
                else
                {
                    tasteImgURL = "gz_sp_58.gif";
                }
                strTastes.AppendFormat("<li><span class=\"kw\"> {0} </span><img width=\"200\" height=\"12\" src=\"images/{1}\">", t["StartKey"], tasteImgURL);
                strTastes.AppendFormat("<span class=\"kw\"> {0} </span></li><li><div  style=\"margin-left: {1}%;width: 13px\"><img width=\"13\" height=\"9\" src=\"images/gz_sp_41.gif\"></div></li>", t["EndKey"], t["Value"]);
                i++;
            }
        }
        lblProTastes.Text = strTastes.ToString();

    }
}